package com.shoe.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shoe.mapper.CollectShowMapper;
import com.shoe.mapper.ShoeCollectMapper;
import com.shoe.pojo.ShoeCollect;
import com.shoe.pojo.ShoeCollectExample;
import com.shoe.pojo.ShoeCollectShow;
import com.shoe.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CollectImpl implements ShoeCollectService {

    @Autowired
    ShoeCollectMapper shoeCollectMapper;

    @Autowired
    CollectShowMapper collectShowMapper;


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public List<ShoeCollectShow> selectAll() {
        return collectShowMapper.selectAll();
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int deleteById(Long collectId) {
        int a = shoeCollectMapper.deleteByPrimaryKey(String.valueOf(collectId));
        return a;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ResponseResult selectAll(int currPage, int pageSize) {
        PageHelper.startPage(currPage,pageSize);
        List<ShoeCollectShow> list = collectShowMapper.selectAll();
        PageInfo<ShoeCollectShow> pageInfo = new PageInfo<>(list);
        pageInfo.setNavigatePages(10);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(pageInfo);
        return responseResult;
    }

    @Override
    public ResponseResult selectByUserId(Long userId, int currPage, int pageSize) {
        PageHelper.startPage(currPage,pageSize);
        List<ShoeCollectShow> list = collectShowMapper.selectByUserId(userId);
        PageInfo<ShoeCollectShow> pageInfo = new PageInfo<>(list);
        pageInfo.setNavigatePages(10);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(pageInfo);
        return responseResult;
    }

    @Override
    public int addCollect(long userId,long CollectId) {

        ShoeCollect shoeCollect = new ShoeCollect();

        shoeCollect.setGoodId(CollectId);
        shoeCollect.setUserId(userId);

        shoeCollectMapper.insert(shoeCollect);

        return 1;
    }

    @Override
    public int deleteCollect(long userId, long CollectId) {
        ShoeCollect shoeCollect = new ShoeCollect();
        shoeCollect.setUserId(userId);
        shoeCollect.setGoodId(CollectId);
        ShoeCollectExample shoeCollectExample = new ShoeCollectExample();
        shoeCollectExample.createCriteria().andUserIdEqualTo(userId).andGoodIdEqualTo(CollectId);
        shoeCollectMapper.deleteByExample(shoeCollectExample);


        return 1;
    }

    @Override
    public int selectId(Long userId, long goodId) {
        ShoeCollectExample shoeCollectExample = new ShoeCollectExample();
        shoeCollectExample.createCriteria().andGoodIdEqualTo(goodId).andUserIdEqualTo(userId);
        List<ShoeCollect> list = shoeCollectMapper.selectByExample(shoeCollectExample);
        if(list != null && list.size() > 0){
            return 1;
        }
        return 0;
    }

}
